<?php
//include file
include("../include/session.php");
require_once('../db_conn/conn_db.php');

$action 	= $_REQUEST['action'];
$id 		= $_REQUEST['id'];
$klien_id	= $_POST['klien_id'];
$tema 	  	= $_POST['tema'];
$judul   	= $_POST['judul'];
$jumlah		= $_POST['jumlah'];
$cover		= $_POST['cover'];
$target 	= $_POST['target'];
$cost 		= $_POST['cost'];
$status 	= $_POST['status'];
$jns_kertas = $_POST['jns_kertas'];
$payment 	= $_POST['payment'];

if ($action=="update") {
	
	if ($_FILES["file"]["error"] > 0)
    {
		$sql_update = "update order_buku set klien_id = '$klien_id', tema_buku = '$tema',
				  judul_buku = '$judul',jml_halaman='$jumlah',cover='$cover',cost='$cost',jns_kertas='$jns_kertas', 
				  payment_status='$payment',
				  status='$status' where id= '$id' ";

		$result_update = mysql_query($sql_update) 
				or die("Gagal Query. Error :".mysql_error());
		echo 
		"<script language=\"JavaScript\">
		alert(\"Data Sukses di Update, tetapi file tidak di upload \" ,\"$_FILES[file][error]\");
		location.href = \"form_order.php?id=$id\"
		</script>";
    }
	else
    {
		/*
		echo "Upload: " . $_FILES["file"]["name"] . "<br>";
		echo "Type: " . $_FILES["file"]["type"] . "<br>";
		echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
		echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
		*/
		if (file_exists("../file/" . $_FILES["file"]["name"]))
		{
			  //echo $_FILES["file"]["name"] . " already exists. ";
			  $file = explode('.',$_FILES["file"]["name"]);
			  $file_ext  = $file['1'];
			  $file_name = $file['0']; 
			  $_FILES["file"]["name"] = $file_name.date('Ymdhis').".".$file_ext;
			
		}
		move_uploaded_file($_FILES["file"]["tmp_name"],"../file/" . $_FILES["file"]["name"]);
		 // echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
		$filename = $_FILES["file"]["name"];
		$sql_update = "update order_buku set klien_id = '$klien_id', tema_buku = '$tema',
				  judul_buku = '$judul',jml_halaman='$jumlah',cover='$cover',cost='$cost',jns_kertas='$jns_kertas', 
				  payment_status='$payment',file_name='$filename',
				  status='$status' where id= '$id' ";
		
		$result_update = mysql_query($sql_update) 
				or die("Gagal Query. Error :".mysql_error());


		echo 	
				"<script language=\"JavaScript\">
				alert(\"Data Sukses di Update \" ,\"Pesan\");
				location.href = \"view_order.php\"
				</script>";
	}
 }

else if ($action=="delete") {
	$sql_delete = "update order_buku set status = '99' where id='$id' ";

//echo "$sql_delete";
//exit;

	$result_delete = mysql_query($sql_delete) 
			or die("Gagal Query. Error :".mysql_error());
	
	
	echo 
					"<script language=\"JavaScript\">
					alert(\"Data Sukses di Hapus\" ,\"Pesan\");
					location.href = \"view_order.php\"
					</script>";
		//echo "delete";
		//exit;
}
else if ($action=="update_pay") {
	$sql_delete = "update order_buku set payment_status = '$payment' where id='$id' ";

//echo "$sql_delete";
//exit;

	$result_delete = mysql_query($sql_delete) 
			or die("Gagal Query. Error :".mysql_error());
	
	
	echo 
					"<script language=\"JavaScript\">
					alert(\"Data Sukses di Update\" ,\"Pesan\");
					location.href = \"view_order.php\"
					</script>";
		//echo "delete";
		//exit;
}
else 
{

  if ($_FILES["file"]["error"] > 0)
    {
		$sql_user = "insert into order_buku values ('','$klien_id','$tema','$judul','$jumlah','$cover','$jns_kertas','$target','$cost',0,2,now(),'')";

		$sql_result = mysql_query($sql_user) 
				or die("Gagal Query. Error :".mysql_error());
		
		echo 
		"<script language=\"JavaScript\">
		alert(\"Data Sukses di Input, tapi file tidak di upload \" ,\"Pesan\");
		location.href = \"view_order.php\"
		</script>";
    }
  else
    {
		/*
		echo "Upload: " . $_FILES["file"]["name"] . "<br>";
		echo "Type: " . $_FILES["file"]["type"] . "<br>";
		echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
		echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
		*/
		if (file_exists("../file/" . $_FILES["file"]["name"]))
		{
			  //echo $_FILES["file"]["name"] . " already exists. ";
			  $file = explode('.',$_FILES["file"]["name"]);
			  $file_ext  = $file['1'];
			  $file_name = $file['0']; 
			  $_FILES["file"]["name"] = $file_name.date('Ymdhis').".".$file_ext;
			
		}
		move_uploaded_file($_FILES["file"]["tmp_name"],"../file/" . $_FILES["file"]["name"]);
		  //echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
		$filename = $_FILES["file"]["name"];
		$sql_user = "insert into order_buku values ('','$klien_id','$tema','$judul','$jumlah','$cover','$jns_kertas','$target','$cost',0,2,now(),'$filename')";
		$sql_result = mysql_query($sql_user) 
		or die("Gagal Query. Error :".mysql_error());
		
		echo 
		"<script language=\"JavaScript\">
		alert(\"Data Sukses di Input \" ,\"Pesan\");
		location.href = \"view_order.php\"
		</script>";
	
    }			
}
		
?>